﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using System.Windows.Resources;
using System.Xml.Linq;
using Microsoft.Phone.Controls;
using System.Collections.Generic;


namespace DreamExplanation
{
    public partial class MainPage : PhoneApplicationPage
    {

        //private List<NavigationInfo> navigationInfos = new List<NavigationInfo>();
        //List<string> keyWrodList = new List<string>();
        // Constructor
        public MainPage()
        {
            
            InitializeComponent();

            LoadingData();

            
        }

        private void LoadingData()
        {
            if (App.dreamCategories != null)
            {
                this.LoadingMessage.Visibility = System.Windows.Visibility.Collapsed;
                
                var dreams = from car
                                in App.dreamCategories.Elements("DreamCategory")
                             select car.Element("Name").Value;

                lbxCarmakers.ItemsSource = dreams;
            }
            else
            {
                LoadingMessage.Visibility = System.Windows.Visibility.Visible;
                LoadingMessage.Text = "无法加载数据......";
            }
        }

        private void lbxCarmakers_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            if (e != null
                && e.AddedItems != null
                && e.AddedItems.Count > 0
                && e.AddedItems[0] != null
                && lbxCarmakers.SelectedIndex!=-1)
            {
                string uri = string.Format("/SubCategoryPage.xaml?categoryName={0}&&contentSearch={1}", e.AddedItems[0],"");
                NavigationService.Navigate(new Uri(uri, UriKind.Relative));
                lbxCarmakers.SelectedIndex = -1;
            }
        }

        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/MainPage.xaml"));
        }

        private void PhoneApplicationPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
        {
            Utility.GoMainPageWithBack(NavigationService, "/MainPage.xaml");
        }

        
    }
}


